package com.heyqing.ems.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyqing.ems.common.MateEnum;
import com.heyqing.ems.common.MemberType;
import com.heyqing.ems.domain.BossInfo;
import com.heyqing.ems.domain.EmployeeInfo;
import com.heyqing.ems.domain.LoginManagement;
import com.heyqing.ems.domain.ManagerInfo;
import com.heyqing.ems.mapper.LoginManagementMapper;
import com.heyqing.ems.po.LoginPo;
import com.heyqing.ems.service.*;
import com.heyqing.ems.util.TokenUtil;
import com.heyqing.ems.vo.AvatarVo;
import com.heyqing.ems.vo.Result;
import lombok.RequiredArgsConstructor;
import org.apache.el.parser.Token;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * ClassName:LoginServiceImpl
 * Package:com.heyqing.ems.service.impl
 * Description:
 *
 * @Date:2024/6/26
 * @Author:Heyqing
 */
@Service
@RequiredArgsConstructor
public class LoginServiceImpl extends ServiceImpl<LoginManagementMapper, LoginManagement> implements LoginService {

    private final MesosphereService mesosphereService;

    @Override
    public Result login(LoginPo loginPo) {
        if (Objects.isNull(loginPo)) {
            return Result.fail();
        }
        String type = loginPo.getType();
        Long username = loginPo.getUsername();
        String password = loginPo.getPassword();
        if (Strings.isBlank(type) || Strings.isBlank(password) || Objects.isNull(username)) {
            return Result.fail();
        }
        if (isFail(type, username, password)) {
            return Result.fail(MateEnum.AUTH_FAIL);
        }
        String token = null;
        try {
            token = TokenUtil.createToken(username +"@"+ type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success(token);
    }

    @Override
    public Result check(String token){
        System.out.println(token);
        if (Strings.isBlank(token)){
            return Result.fail();
        }
        Long id = 0L;
        String type = "";
        try {
            String auth = TokenUtil.parseToken(token);
            if (Strings.isBlank(auth)){
                return Result.fail();
            }
            String[] split = auth.split("@");
            id = Long.parseLong(split[0]);
            type = split[1];
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        LoginManagement member = lambdaQuery()
                .eq(LoginManagement::getType, type)
                .eq(LoginManagement::getUsername, id)
                .one();
        if (Objects.isNull(member)){
            return Result.fail();
        }
        AvatarVo avatarVo = mesosphereService.getIdAndName(type,member.getUsername());
        return Result.success(avatarVo);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        LoginManagement member = lambdaQuery()
                .eq(LoginManagement::getUsername, id)
                .one();
        removeById(member);
    }

    /****************************************************private****************************************************/

    /**
     * 是否登陆成功
     *
     * @param type
     * @param username
     * @param password
     * @return
     */
    private boolean isFail(String type, Long username, String password) {
        LoginManagement member = lambdaQuery()
                .eq(LoginManagement::getType, type)
                .eq(LoginManagement::getUsername, username)
                .eq(LoginManagement::getPassword, password)
                .one();
        if (Objects.isNull(member)){
            return true;
        }
        return false;
    }
}
